﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InChatter.Client.Service
{
    public class DataHelper
    {
        #region Save
        public static bool Save(ChatServer.InChatterMessage message)
        {
            try
            {
                lock (string.Intern(ChatMsgCenter.Instance.DbUrl))
                {
                    using (var db = NDatabase.OdbFactory.Open(ChatMsgCenter.Instance.DbUrl))
                    {
                        db.Store(message);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Save(IEnumerable<ChatServer.InChatterMessage> msgList)
        {
            try
            {
                lock (string.Intern(ChatMsgCenter.Instance.DbUrl))
                {
                    using (var db = NDatabase.OdbFactory.Open(ChatMsgCenter.Instance.DbUrl))
                    {
                        foreach (var msg in msgList)
                        {
                            db.Store(msg);
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        public static bool Update(ChatServer.InChatterMessage message)
        {
            try
            {
                lock (string.Intern(ChatMsgCenter.Instance.DbUrl))
                {
                    using (var db = NDatabase.OdbFactory.Open(ChatMsgCenter.Instance.DbUrl))
                    {
                        var msg = (from item in db.AsQueryable<ChatServer.InChatterMessage>()
                                   where item.ID == message.ID
                                   select item).Single();
                        msg.IsRead = message.IsRead;
                        db.Store(msg);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Delete(string msgId)
        {
            try
            {
                lock (string.Intern(ChatMsgCenter.Instance.DbUrl))
                {
                    using (var db = NDatabase.OdbFactory.Open(ChatMsgCenter.Instance.DbUrl))
                    {
                        var msg = (from item in db.AsQueryable<ChatServer.InChatterMessage>()
                                   where item.ID == msgId
                                   select item).Single();
                        db.Delete(msg);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void ReadMsg(ChatServer.InChatterMessage msg)
        {
            Update(msg);
        }
    }
}
